using _9GrpcServer;
using Microsoft.OpenApi.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.Sources.Clear();
builder.Configuration.SetBasePath(AppContext.BaseDirectory);
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false);

// enable detailed errors in gRPC response
builder.Services.AddGrpc(options => options.EnableDetailedErrors = true);

// Swashbuckle.AspNetCore
builder.Services.AddMvc();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "1.0" });
    c.EnableAnnotations(true, true);
    c.UseAllOfForInheritance();
    c.SchemaGeneratorOptions.SubTypesSelector = SwaggerTools.GetDataContractKnownTypes;
});

// enable ServiceModel.Grpc
builder.Services.AddServiceModelGrpc();
builder.Services.AddServiceModelGrpcSwagger();
    
var app = builder.Build();
app.UseRouting();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("v1/swagger.json", "1.0");
});
app.UseServiceModelGrpcSwaggerGateway();
app.MapGrpcService<UserService>();

app.Run();
return;


public class UserService : IUserService
{
    public Task<LoginResponse> Login(LoginRequest request)
    {
        return Task.FromResult(new LoginResponse() { Success = true, Message = $"{request.Username}登录成功" });
    }
}